Javatpoint Logo
Javatpoint Logo

Setter Injection with Collection Example

We can inject collection values by setter method in spring framework. There can be used three elements inside the property element.

It can be:
  1. list
  2. set
  3. map
Each collection can have string based and non-string based values.

In this example, we are taking the example of Forum where One question can have multiple answers. There are three pages:

  1. Question.java
  2. applicationContext.xml
  3. Test.java

In this example, we are using list that can have duplicate elements, you may use set that have only unique elements. But, you need to change list to set in the applicationContext.xml file and List to Set in the Question.java file.

Question.java

This class contains three properties with setters and getters and displayInfo() method that prints the information. Here, we are using List to contain the multiple answers.

Video Player is loading.
Current Time 4:20
Duration 4:57
Loaded: 100.00%
Stream Type LIVE
Remaining Time 0:37
 
1x
    • Chapters
    • descriptions off, selected
    • captions off, selected

      1. package com.javatpoint;  
      2. import java.util.Iterator;  
      3. import java.util.List;  
      4.   
      5. public class Question {  
      6. private int id;  
      7. private String name;  
      8. private List<String> answers;  
      9.   
      10. //setters and getters  
      11.   
      12. public void displayInfo(){  
      13.     System.out.println(id+" "+name);  
      14.     System.out.println("answers are:");  
      15.     Iterator<String> itr=answers.iterator();  
      16.     while(itr.hasNext()){  
      17.         System.out.println(itr.next());  
      18.     }  
      19. }  
      20.   
      21. }  
      applicationContext.xml

      The list element of constructor-arg is used here to define the list.

      1. <?xml version="1.0" encoding="UTF-8"?>  
      2. <beans  
      3.     xmlns="http://www.springframework.org/schema/beans"  
      4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
      5.     xmlns:p="http://www.springframework.org/schema/p"  
      6.     xsi:schemaLocation="http://www.springframework.org/schema/beans   
      7. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">  
      8.   
      9. <bean id="q" class="com.javatpoint.Question">  
      10. <property name="id" value="1"></property>  
      11. <property name="name" value="What is Java?"></property>  
      12. <property name="answers">  
      13. <list>  
      14. <value>Java is a programming language</value>  
      15. <value>Java is a platform</value>  
      16. <value>Java is an Island</value>  
      17. </list>  
      18. </property>  
      19. </bean>  
      20.   
      21. </beans>  
      Test.java

      This class gets the bean from the applicationContext.xml file and calls the displayInfo method.

      1. package com.javatpoint;  
      2.   
      3. import org.springframework.beans.factory.BeanFactory;  
      4. import org.springframework.beans.factory.xml.XmlBeanFactory;  
      5. import org.springframework.core.io.ClassPathResource;  
      6. import org.springframework.core.io.Resource;  
      7.   
      8. public class Test {  
      9. public static void main(String[] args) {  
      10.     Resource r=new ClassPathResource("applicationContext.xml");  
      11.     BeanFactory factory=new XmlBeanFactory(r);  
      12.       
      13.     Question q=(Question)factory.getBean("q");  
      14.     q.displayInfo();  
      15.       
      16. }  
      17. }  





      Youtube For Videos Join Our Youtube Channel: Join Now

      Feedback

      • Send your Feedback to feedback@javatpoint.com

      Help Others, Please Share

      facebook
      twitter
      pinterest

      Learn Latest Tutorials


      Preparation


      Trending Technologies


      B.Tech / MCA